package com.uphone.kingmall.view.cityseletor;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.LogUtils;
import com.china.zhuoyue.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.uphone.kingmall.utils.PopupUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


/**
 * @作者 radish
 * @创建日期 2019/2/28 3:00 PM
 * @邮箱 15703379121@163.com
 * @描述
 */
public class CitySelectorUtil {

    private static Integer x;
    private static Integer y;

    private static String a, b, c;
    private static PopupUtil popupWindow;

    public interface CitySelectorCallBack {
        void callBack(String province, String city, String district);
    }

    public static boolean isShowing(boolean isClose) {
        if (popupWindow != null) {
            if (isClose) {
                popupWindow.dismiss();
            }
            return true;
        }
        return false;
    }

    public static void citySelector(Activity activity, final CitySelectorCallBack callBack) {
        View layout = View.inflate(activity, R.layout.kingmall_picker_view0, null);
        popupWindow = PopupUtil.getInstance(activity, layout).showBottom();

        PickerScrollView pickerscrlllview1 = layout.findViewById(R.id.pickerscrlllview1);
        final PickerScrollView pickerscrlllview2 = layout.findViewById(R.id.pickerscrlllview2);
        final PickerScrollView pickerscrlllview3 = layout.findViewById(R.id.pickerscrlllview3);
        try {
            layout.findViewById(R.id.picker_yes).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LogUtils.e("a,b,c:" + a + b + c);
                    callBack.callBack(a, b, c);
                    if (popupWindow != null) {
                        popupWindow.dismiss();
                    }
                }
            });

            InputStream is = activity.getAssets().open("address.json");
            String text = readTextFromSDcard(is);
            LogUtils.e("textStr:"+text);
            Gson gson = new Gson();
            final List<CitySelectorJson> city_list = gson.fromJson(text, new TypeToken<List<CitySelectorJson>>() {}.getType());
//            final CitySelectorJson city_lists = gson.fromJson(text, CitySelectorJson.class);
//            final List<CitySelectorJson.ChildsBeanXX> city_list = city_lists.getChilds();
            LogUtils.e("____" + city_list.size() + "");

            ArrayList list1 = new ArrayList<Pickers>();
            for (int i = 0; i < city_list.size(); i++) {
                list1.add(new Pickers(city_list.get(i).getName(), i + ""));

                if (i == 0) {
                    a = city_list.get(i).getName();
                }
            }
            pickerscrlllview1.setData(list1);
            pickerscrlllview1.setSelected(0);

            final ArrayList list2 = new ArrayList<Pickers>();
            for (int i = 0; i < city_list.get(0).getChilds().size(); i++) {
                list2.add(new Pickers(city_list.get(0).getChilds().get(i).getName(), i + ""));

                if (i == 0) {
                    b = city_list.get(0).getChilds().get(i).getName();
                }
            }
            pickerscrlllview2.setData(list2);
            pickerscrlllview2.setSelected(0);


            final ArrayList list3 = new ArrayList<Pickers>();
            if (city_list.get(0).getChilds().get(0).getName() != null) {
                for (int i = 0; i < city_list.get(0).getChilds().get(0).getChilds().size(); i++) {
                    list3.add(new Pickers(city_list.get(0).getChilds().get(0).getChilds().get(i).getName(), i + ""));
                    if (i == 0) {
                        c = city_list.get(0).getChilds().get(0).getChilds().get(i).getName();
                    }
                }
            }
            pickerscrlllview3.setData(list3);
            if (list3 != null && list3.size() > 0) {
                pickerscrlllview3.setSelected(0);
            }
            pickerscrlllview1.setOnSelectListener(new PickerScrollView.onSelectListener() {
                @Override
                public void onSelect(Pickers pickers) {
                    try {
                        x = Integer.parseInt(pickers.getShowId());
                        a = pickers.getShowConetnt();
                        list2.clear();
                        for (int i = 0; i < city_list.get(x).getChilds().size(); i++) {
                            list2.add(new Pickers(city_list.get(x).getChilds().get(i).getName(), i + ""));

                            if (i == 0) {
                                b = city_list.get(x).getChilds().get(i).getName();
                            }
                        }
                        pickerscrlllview2.setData(list2);
                        pickerscrlllview2.setSelected(0);

                        y = 0;
                        list3.clear();
                        c = "";
                        if (city_list.get(x).getChilds().get(y).getChilds() != null)
                            if ( city_list.get(x).getChilds().get(y).getChilds().size() == 1 && TextUtils.equals( city_list.get(x).getChilds().get(y).getName(), city_list.get(x).getChilds().get(y).getChilds().get(0).getName())){
                                for (int i = 0; i < city_list.get(x).getChilds().get(y).getChilds().get(0).getChilds().size(); i++) {
                                    list3.add(new Pickers(city_list.get(x).getChilds().get(y).getChilds().get(0).getChilds().get(i).getName(), i + ""));
                                    if (i == 0) {
                                        c = city_list.get(x).getChilds().get(y).getChilds().get(0).getChilds().get(i).getName();
                                    }
                                }
                            }else {
                                for (int i = 0; i < city_list.get(x).getChilds().get(y).getChilds().size(); i++) {
                                    list3.add(new Pickers(city_list.get(x).getChilds().get(y).getChilds().get(i).getName(), i + ""));
                                    if (i == 0) {
                                        c = city_list.get(x).getChilds().get(y).getChilds().get(i).getName();
                                    }
                                }
                            }
                        pickerscrlllview3.setData(list3);
                        if (list3 != null && list3.size() > 0) {
                            pickerscrlllview3.setSelected(0);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            pickerscrlllview2.setOnSelectListener(new PickerScrollView.onSelectListener() {
                @Override
                public void onSelect(Pickers pickers) {
                    try {
                        y = Integer.parseInt(pickers.getShowId());
                        b = pickers.getShowConetnt();
                        list3.clear();
                        c = "";
                        if (city_list.get(x).getChilds().get(y).getChilds() != null)
                            if ( city_list.get(x).getChilds().get(y).getChilds().size() == 1 && TextUtils.equals( city_list.get(x).getChilds().get(y).getName(), city_list.get(x).getChilds().get(y).getChilds().get(0).getName())){
                                for (int i = 0; i < city_list.get(x).getChilds().get(y).getChilds().get(0).getChilds().size(); i++) {
                                    list3.add(new Pickers(city_list.get(x).getChilds().get(y).getChilds().get(0).getChilds().get(i).getName(), i + ""));
                                    if (i == 0) {
                                        c = city_list.get(x).getChilds().get(y).getChilds().get(0).getChilds().get(i).getName();
                                    }
                                }
                            }else {
                                for (int i = 0; i < city_list.get(x).getChilds().get(y).getChilds().size(); i++) {
                                    list3.add(new Pickers(city_list.get(x).getChilds().get(y).getChilds().get(i).getName(), i + ""));
                                    if (i == 0) {
                                        c = city_list.get(x).getChilds().get(y).getChilds().get(i).getName();
                                    }
                                }
                            }
                        pickerscrlllview3.setData(list3);
                        if (list3 != null && list3.size() > 0)
                            pickerscrlllview3.setSelected(0);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            pickerscrlllview3.setOnSelectListener(new PickerScrollView.onSelectListener() {
                @Override
                public void onSelect(Pickers pickers) {
                    try {
                        c = pickers.getShowConetnt();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String readTextFromSDcard(InputStream is) throws Exception {
        InputStreamReader reader = new InputStreamReader(is, "utf-8");
        BufferedReader bufferedReader = new BufferedReader(reader);
        StringBuffer buffer = new StringBuffer("");
        String str;
        while ((str = bufferedReader.readLine()) != null) {
            buffer.append(str);
            buffer.append("\n");
        }
        return buffer.toString();//把读取的数据返回
    }

}
